/** @Date 上午9:43:53
  * @version 1.0.0
  * @author shangyd
  * Copyright (c) 2015
  */
package com.sdw.soft.test.zookeeper.masterslave;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sdw.soft.test.zookeeper.SimulateClient;

public class SimulateClient3 extends SimulateClient implements Watcher{

	private static final Logger logger = LoggerFactory.getLogger(SimulateClient3.class);
	
	private static final String CHILDPATH = "serverA";
	
	public SimulateClient3(String host, String description) throws Exception {
		super(host, description);
	}

	@Override
	public void process(WatchedEvent event) {
		if(event.getType() == Event.EventType.NodeDeleted){
			try {
				logger.info("find server has down ... ...");
				check(CHILDPATH,this);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public void createParent(){
		try {
			zk.create(PATH, "parent root".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		} catch (KeeperException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		try {
			SimulateClient3 s3 = new SimulateClient3("192.168.183.133:2181", CHILDPATH);
//			s3.createParent();
			s3.createServer(PATH + "/" + PCHILDPATH,CHILDPATH);
			s3.check(CHILDPATH, s3);
			Thread.sleep(Long.MAX_VALUE);
			s3.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

}
